package Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import Dao.ClassDao;
import Dao.CourseDao;
import Dao.ScoreDao;
import Dao.StudentDao;
import Model.Course;
import Model.Score;
import Model.Student;
import Model.queryclass;
import Util.PrintUtil;

public class MySQLDemo {
	private static ArrayList<Student> studentList = new ArrayList<>();
	private static ArrayList<queryclass> classList = new ArrayList<>();
	private static ArrayList<Score> scoreList = new ArrayList<>();
	private static ArrayList<Course> CourseList = new ArrayList<>();
	static boolean isMenuShow = false;

	public static void main(String[] args) {
		showMenu();
	}

	private static void showMenu() {
		while (true) {
			// 为了避免程序疯狂运行造成死机
			// 每秒钟检查一次是否要展示菜单
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			if (!isMenuShow) {
				PrintUtil.printMenu();
				isMenuShow = true;

				Scanner sc = new Scanner(System.in);
				int i;
				try {
					i = sc.nextInt();
					switch (i) {
					case 1:
						studentList = StudentDao.queryStudentList();
						PrintUtil.print(studentList);
						break;
					case 2:
						classList = ClassDao.queryClassList();
						PrintUtil.displayclass(classList);
						break;
					case 3:
						scoreList = ScoreDao.scoreList();
						PrintUtil.displayscore(scoreList);
						break;
					case 4:
						CourseList = CourseDao.CourseList();
						PrintUtil.displayCourse(CourseList);
						break;
					case 5:
						// 获取学生信息
						Student student = getStudentInfo();
						// 添加学生
						// StudentDao.addUseStatement(student);
						StudentDao.addUsePrepareStatement(student);
						break;
					case 6:
						// 获取用户输入的学生姓名
						String name = getStudentName();
						// 按照姓名删除学生
						StudentDao.del(name);
						break;
					case 7:
						// 获取课程信息
						Course course = getCourseInfo();
						// 添加课程
						CourseDao.addUsePrepareStatement(course);
						break;
					case 8:
						// 获取用户输入的课程名
						String Coursename = getCoursename();
						// 按照课程名删除课程
						CourseDao.del(Coursename);
						break;
					case 9:
						// 获取学生成绩所属信息
						Score scoreinfo = getScoreInfo();
						// 添加成绩
						ScoreDao.addUsePrepareStatement(scoreinfo);
						break;
					case 10:
						// 获取用户输入的学生姓名
						name = getStudentName();
						Coursename = getCoursename();
						// 按照姓名删除学生成绩
						ScoreDao.del(name, Coursename);
						break;
					case 11:
						// 获取班级信息
						queryclass clazz = getClazzInfo();
						// 添加班级
						ClassDao.addUsePrepareStatement(clazz);
						break;
					case 12:
						// 获取用户输入的班级名
						String classname = getClassname();
						// 按照班级名删除班级
						ClassDao.del(classname);
						break;
					case 13:
						// 获取需要修改学生的姓名
						name = getStudentName();
						// 获取要修改的字段
						int column = getUpdateColumn();
						// 获取要修改的值
						String value = getNewValue();
						// 更新学生表
						StudentDao.update(name, column, value);
						break;
					case 14:
						// 获取需要修改课程名
						Coursename = getCoursename();
						// 获取要修改的字段
						column = getUpdateColumnCourse();
						// 获取要修改的值
						value = getNewValue();
						// 更新学生表
						CourseDao.update(Coursename, column, value);
						break;
					case 15:
						// 获取需要修改班级名
						classname = getClassname();
						// 获取要修改的字段
						column = getUpdateColumnClass();
						// 获取要修改的值
						value = getNewValue();
						// 更新学生表
						ClassDao.update(classname, column, value);
						break;
					case 16:
						// 获取需要修改学生姓名和课程名
						name = getStudentName();
						Coursename = getCoursename();
						// 获取要修改的字段
						column = getUpdateColumnScore();
						// 获取要修改的值
						value = getNewValue();
						// 更新学生表
						ScoreDao.update(name, Coursename, column, value);
						break;
					case 17:
						// 实现选课
						// 获取用户的成绩编号
						int scoreid = getScoreid();
						// 获取要修改的学生信息
						name = getStudentName();
						// 通过学生姓名获取到学生的ID
						int id = StudentDao.getStuIdWithName(name);
						// 通过CourseDao获取课程列表
						ArrayList<Course> courseList = CourseDao.queryCourseList();
						// 打印课程列表
						CourseList = CourseDao.CourseList();
						PrintUtil.displayCourse(CourseList);
						// 获取用户选择的课程ID
						int Courseid = getSeletedCouseID();
						// 获取用户选择的课程名
						Coursename = getCoursename();
						// 获取用户的班级编号
						int classid = getClassid();
						// 获取学生的选课成绩
						int Score = getCourseScore();
						// 插入选课和成绩
						ScoreDao.add(scoreid, name, id, Coursename, Courseid, classid, Score);
						break;
					case 18:
						// 查询选课信息
						// 查看成绩
						// 获取学生姓名
						name = getStudentName();
						int stuid = StudentDao.getStuIdWithName(name);
						// 通过id查询该学生的成绩列表
						ArrayList<Score> scoreList = ScoreDao.getScoreList(stuid);
						// 打印成绩列表
						PrintUtil.displayscore(scoreList);
						break;
					case 19:
						// 获取课程名
						Coursename = getCoursename();
						// 返回该课程的学生成绩排名
						ArrayList<Score> scoreList2 = ScoreDao.order(Coursename);
						// 打印排名
						PrintUtil.displayScoreSort(scoreList2);
						break;
					case 20:
						// 查询所有课程最高分、最低分、平均分、不及格人数
						List<Map<String, Object>> mapList = ScoreDao.getDetail();
						PrintUtil.displayDetail(mapList);
						break;
					case 21:
						// 退出程序，关闭数据库连接
						System.out.println("成功退出管理系统!");
						System.exit(0);
						break;
					default:
						System.out.println("您的输入有误，请重新输入");
						break;
					}

				} catch (Exception e) {
					System.out.println("您的输入有误，请重新输入");
				}

				isMenuShow = false;
			}
		}
	}

	private static int getScoreid() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入成绩编号");
		int value = in.nextInt();
		return value;
	}

	private static int getClassid() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入班级编号");
		int value = in.nextInt();
		return value;
	}

	private static int getCourseScore() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入课程成绩");
		int value = in.nextInt();
		return value;
	}

	private static int getSeletedCouseID() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入课程ID");
		int value = in.nextInt();
		return value;
	}

	private static int getUpdateColumnScore() {
		Scanner in = new Scanner(System.in);
		System.out.println("请选择要更新的字段");
		System.out.println("1.学生姓名");
		System.out.println("2.学号");
		System.out.println("3.课程名");
		System.out.println("4.课程号");
		System.out.println("5.班级号");
		System.out.println("6.分数");
		int result = in.nextInt();
		return result;
	}

	private static int getUpdateColumnClass() {
		Scanner in = new Scanner(System.in);
		System.out.println("请选择要更新的字段");
		System.out.println("1.班级编号");
		System.out.println("2.班级名");
		System.out.println("3.年级");
		System.out.println("4.专业");
		int result = in.nextInt();
		return result;
	}

	private static int getUpdateColumnCourse() {
		Scanner in = new Scanner(System.in);
		System.out.println("请选择要更新的字段");
		System.out.println("1.课程号");
		System.out.println("2.课程名");
		int result = in.nextInt();
		return result;
	}

	private static String getNewValue() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入新的值");
		String value = in.nextLine();
		return value;
	}

	private static int getUpdateColumn() {
		Scanner in = new Scanner(System.in);
		System.out.println("请选择要更新的字段");
		System.out.println("1.姓名");
		System.out.println("2.年龄");
		System.out.println("3.班级名");
		System.out.println("4.专业");
		System.out.println("5.班级编号");
		int result = in.nextInt();
		return result;
	}

	private static String getClassname() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入班级名");
		String classname = in.nextLine();
		return classname;
	}

	private static queryclass getClazzInfo() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入班级编号");
		int classid = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入班级名称");
		String classname = in.nextLine();

		in = new Scanner(System.in);
		System.out.println("请输入班级所属年级");
		int year = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入专业名");
		String major = in.nextLine();

		System.out.println("班级信息输入完成");

		// 智能提示自动完成 ALT+？
		queryclass clazz = new queryclass(classid, classname, year, major);
		return clazz;
	}

	private static Score getScoreInfo() {

		Scanner in = new Scanner(System.in);
		System.out.println("请输入成绩编号");
		int scoreid = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入学生姓名");
		String name = in.nextLine();

		in = new Scanner(System.in);
		System.out.println("请输入学号");
		int id = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入课程名");
		String Coursename = in.nextLine();

		in = new Scanner(System.in);
		System.out.println("请输入课程号");
		int Courseid = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入班级编号");
		int classid = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入成绩");
		int score = in.nextInt();

		System.out.println("成绩信息输入完成");

		// 智能提示自动完成 ALT+？
		Score scoreinfo = new Score(scoreid, name, id, Coursename, Courseid, classid, score);
		return scoreinfo;
	}

	private static String getCoursename() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入课程名");
		String Coursename = in.nextLine();
		return Coursename;
	}

	private static Course getCourseInfo() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入课程号");
		int Courseid = in.nextInt();

		in = new Scanner(System.in);
		System.out.println("请输入课程名");
		String Coursename = in.nextLine();

		System.out.println("课程信息输入完成");

		// 智能提示自动完成 ALT+？
		Course course = new Course(Courseid, Coursename);
		return course;
	}

	private static String getStudentName() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入姓名");
		String name = in.nextLine();
		return name;
	}

	private static Student getStudentInfo() {
		Scanner in = new Scanner(System.in);
		int id = 0, age = 0;
		boolean isNo = true;
		boolean isAge = true;
		System.out.println("请输入学号:");
		while (isNo) {
			try {
				id = in.nextInt();
				isNo = false;
			} catch (Exception ex) {
				System.out.println("您输入的不是正常学号，请重新输入:");
				in.nextLine();
			}
		}

		in.nextLine();
		System.out.println("请输入姓名");
		String name = in.nextLine();

		System.out.println("请输入年龄");
		while (isAge) {
			try {
				age = in.nextInt();
				isAge = false;
			} catch (Exception ex) {
				System.out.println("您输入的不是正常年龄，请重新输入:");
				in.nextLine();
			}
		}

		in.nextLine();
		System.out.println("请输入班级名称");
		String classname = in.nextLine();

		System.out.println("请输入专业");
		String major = in.nextLine();

		/*
		 * in = new Scanner(System.in); System.out.println("请输入班级编号"); int
		 * classid = in.nextInt();
		 */

		System.out.println("学生信息输入完成");

		// 智能提示自动完成 ALT+？
		int classid = ClassDao.getClassId(classname);
		Student student = new Student(id, name, age, classname, major, classid);
		return student;
	}
}